﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace msilgc.driver.cuda {
    public class CudaNativeHelper {
        [DllImport("msilgc.cudahelper")]
        public static extern int GetFastestCudaDeviceId();
        
        [DllImport("msilgc.cudahelper")]
        private static extern int _GetNumberOfCudaDevicesAvailable();

        private static int? _numberOfDevicesAvailable = null;
        public static int GetNumberOfCudaDevicesAvailable()
        {
            if(!_numberOfDevicesAvailable.HasValue)
            {
                _numberOfDevicesAvailable = _GetNumberOfCudaDevicesAvailable();
            }
            return _numberOfDevicesAvailable.Value;
        }

        [DllImport("msilgc.cudahelper")]
        private static extern IntPtr _GetDeviceName(int deviceId);

        private static readonly Dictionary<int, string> _names = new Dictionary<int, string>();
        public static string GetDeviceName(int deviceId)
        {
            string name;
            if (!_names.ContainsKey(deviceId))
            {
                IntPtr namePtr = _GetDeviceName(deviceId);
                name = Marshal.PtrToStringAnsi(namePtr);
                _names.Add(deviceId, name);
            }
            else
            {
                name = _names[deviceId];
            }
            return name;
        }
    }
}
